package com.zk.algorithm.bigdata;

import java.io.File;
import java.io.IOException;

/**
 * 海量日志数据，提取出某日访问百度次数最多的那个IP
 *
 * 随机 IP 生成器: https://www.browserling.com/tools/random-ip
 */
public class ExtractMostVisitedIP {

    public static final String TEMP_FOLDER_PATH = "./temp/bigdata/ip/";
    public static final String TEMP_IP_FILE_PREFIX = "ip-";
    public static final String TEMP_IP_FILE_SUFFIX = ".txt";

    public static void main(String...args) throws IOException {
        FileUtils.removeAllFilesInFolder(new File(TEMP_FOLDER_PATH));

        File file = new File(ExtractMostVisitedIP.class.getClassLoader().getResource("bigdata/ip.txt").getFile());
        new ExtractMostVisitedIP().extract(file, 10000, 4);
    }

    /**
     * @param logFile 日志文件
     * @param maxLineNumber 最大行数
     * @param maxLineMemoryCanHold 内存最大支持存放几行
     * @return 访问百度次数最多的 IP
     */
    public String extract(File logFile, int maxLineNumber, int maxLineMemoryCanHold) throws IOException {
        int numberOfIPFile = maxLineNumber / maxLineMemoryCanHold;

        FileUtils.readFileLineByLine(logFile, new FileUtils.ReadListener() {

            @Override
            public void onRead(String line, int lineNumber) {
                int fileNumber = Math.abs(line.hashCode()) % numberOfIPFile;
                File file = new File(TEMP_FOLDER_PATH + TEMP_IP_FILE_PREFIX + fileNumber + TEMP_IP_FILE_SUFFIX);

                try {
                    FileUtils.appendTo(file, line);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onReadCompleted() {

            }

        });

        return null;
    }

}
